package com.zgr.ssm.controller;

import com.zgr.ssm.entity.FileEntity;
import com.zgr.ssm.entity.ResponseResult;
import com.zgr.ssm.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService fileService;
    /**
     * 上传文件
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload.do", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<String> uploadFile(HttpServletRequest request,HttpSession session, @RequestParam("file") CommonsMultipartFile file){
        //判断是否为一个文件
        if(file.isEmpty()){
            return new ResponseResult<>(300,"请选择一个文件");
        }
        //设置上传文件的文件夹
        String uploadDir = "upload";
        //获取文件夹路径
        String dirPath = request.getSession().getServletContext().getRealPath(uploadDir);
        //String dirPath = request.getContextPath() + "/" + uploadDir;
        System.out.println("dirPath: " + dirPath);
        File parentDir = new File(dirPath);
        //获取原始文件名
        String fileName = file.getOriginalFilename();
        //上传文件的对象
        File desc = new File(parentDir, fileName);
        try {
            //上传文件
            file.transferTo(desc);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult<String>(400,"上传失败！请联系管理员");
        }

        //上传文件在服务器的路径
        String url = dirPath + "\\" + fileName;
        System.out.println("文件存储路径：" + url);
        FileEntity data = new FileEntity();
        data.setUserId(getUid(session));
        data.setFileName(fileName);
        data.setUrl(url);
        fileService.upload(data);

        return new ResponseResult<String>(200,"上传成功");
    }


    @RequestMapping(value = "/showFile.do", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<List<FileEntity>> findFile(HttpSession session){
        System.out.println(session);
        Integer uid = getUid(session);
        ResponseResult<List<FileEntity>> rr =  new ResponseResult<List<FileEntity>>();
        List<FileEntity> data = fileService.getList(uid);
        rr.setState(200);
        rr.setData(data);
        return rr;
    }

    private Integer getUid(HttpSession session){
        //根据session 获取用户id
        System.out.println("session:" + session.getAttribute("uid"));
        Integer uid = Integer.valueOf(session.getAttribute("uid").toString());
        System.out.println(uid);
        return uid;
    }
}
